package com.zx.oa.dao.marketTypeClass;

import com.zx.oa.dao.BaseDao;
import com.zx.oa.entity.MarketClass;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MarketTypeClassDaoImpl extends BaseDao implements MarketTypeClassDao {

    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    @Override
    public List<MarketClass> getMarketClassInfo() {
        List<MarketClass> marketClassList=new ArrayList<>();
        conn=this.getConnection();
        String sql="SELECT * FROM marketclass";
        try {
            pstmt=conn.prepareStatement(sql);
            rs=pstmt.executeQuery();
            while (rs.next()){
                Integer id=rs.getInt(1);
                String name=rs.getString(2);
                String type=rs.getString(3);
                MarketClass marketClass=new MarketClass(id,name,type);
                marketClassList.add(marketClass);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return marketClassList;
    }

    @Override
    public List<String> getClassTypeAll() {
        List<String> list=new ArrayList<>();
        conn=this.getConnection();
        String sql="SELECT DISTINCT classType FROM marketclass";
        try {
            pstmt=conn.prepareStatement(sql);
            rs=pstmt.executeQuery();
            while (rs.next()){
                list.add(rs.getString(1));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public MarketClass getMarketClassInfoByClassName(String className) {
        MarketClass marketClass=null;
        conn=this.getConnection();
        String sql="SELECT * FROM marketclass WHERE classname=?";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,className);
            rs=pstmt.executeQuery();
            if(rs.next()){
                Integer id=rs.getInt(1);
                String claName=rs.getString(2);
                String claType=rs.getString(3);
                marketClass=new MarketClass(id,claName,claType);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return marketClass;
    }

    @Override
    public List<MarketClass> getMarketClassInfoByType(String stuType) {
        List<MarketClass> marketClassList=new ArrayList<>();
        conn=this.getConnection();
        String sql="SELECT * FROM marketclass WHERE classType=?";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,stuType);
            rs=pstmt.executeQuery();
            while (rs.next()){
                Integer id=rs.getInt(1);
                String claName=rs.getString(2);
                String claType=rs.getString(3);
                MarketClass marketClass=new MarketClass(id,claName,claType);
                marketClassList.add(marketClass);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return marketClassList;
    }
}
